package com.client;


import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.shared.FieldVerifier;
import com.google.gwt.user.client.ui.CaptionPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Mbi implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	private final DnaServiceAsync dnaService = GWT.create(DnaService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final Label errorLabel = new Label();

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel rootPanel = RootPanel.get("panel");
		RootPanel.get("errorLabelContainer").add(errorLabel);

		VerticalPanel verticalPanel = new VerticalPanel();
		rootPanel.add(verticalPanel);
		verticalPanel.setSize("430px", "446px");

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		final TextArea nameField = new TextArea();
		horizontalPanel.add(nameField);
		nameField.setText("GWT User");

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		final Button sendButton = new Button("Send");
		horizontalPanel.add(sendButton);

		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		final TabPanel tabPanel = new TabPanel();
		verticalPanel.add(tabPanel);
		tabPanel.setSize("100%", "100%");
		// tabPanel.setSize("376px", "312px");

		final FlexTable flexTable = new FlexTable();
		tabPanel.add(flexTable, "Wprowadź dane", false);
		// flexTable.setSize("100%", "100%");
		final TextBox numberOfMarkers = new TextBox();
		flexTable.setWidget(0, 0, numberOfMarkers);
		// numberOfMarkers.setWidth("63px");

		final Button sendButtonNumberOfMarkers = new Button("Wy");
		flexTable.setWidget(0, 1, sendButtonNumberOfMarkers);

		final FlexTable flexTable_1 = new FlexTable();
		tabPanel.add(flexTable_1, "Wczytaj plik", false);
		flexTable_1.setSize("5cm", "3cm");

		FileUpload fileUpload = new FileUpload();
		flexTable_1.setWidget(0, 0, fileUpload);
		
		Button btnWy = new Button("Wyślij do analizy");
		verticalPanel.add(btnWy);
		
		Label lblWynikAnalizy = new Label("Wynik analizy");
		verticalPanel.add(lblWynikAnalizy);
		
		CaptionPanel cptnpnlNewPanel = new CaptionPanel("New panel");
		verticalPanel.add(cptnpnlNewPanel);
		
		CaptionPanel cptnpnlNewPanel_1 = new CaptionPanel("New panel");
		verticalPanel.add(cptnpnlNewPanel_1);

		// Create the popup dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendNameToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendNameToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void sendNameToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				String textToServer = nameField.getText();
				if (!FieldVerifier.isValidName(textToServer)) {
					errorLabel.setText("Please enter at least four characters");
					return;
				}

				// Then, we send the input to the server.
				sendButton.setEnabled(false);
				textToServerLabel.setText(textToServer);
				serverResponseLabel.setText("");
				greetingService.greetServer(textToServer,
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox
										.setText("Remote Procedure Call - Failure");
								serverResponseLabel
										.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR);
								dialogBox.center();
								closeButton.setFocus(true);
							}

							public void onSuccess(String result) {
								dialogBox.setText("Remote Procedure Call");
								serverResponseLabel
										.removeStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(result);
								dialogBox.center();
								closeButton.setFocus(true);
							}
						});
			}
		}

		// Create a handler for the sendButton and nameField
		class DnaHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				send();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					send();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void send() {
				// First, we validate the input.
				errorLabel.setText("");
				
			//	int a = tabPanel.
				boolean b1 = flexTable_1.isVisible();
				boolean b2 = flexTable.isVisible();
				 Logger rootLogger = Logger.getLogger("");
				 rootLogger.log(Level.SEVERE, "pageIndex selected: " + b1+" "+b2);
				//log
				String textToServer = nameField.getText();
				if (!FieldVerifier.isValidName(textToServer)) {
					errorLabel.setText("Podaj prawidlowa mieszanine");
					return;
				}

				// Then, we send the input to the server.
				sendButton.setEnabled(false);
				textToServerLabel.setText(textToServer);
				serverResponseLabel.setText("");
				dnaService.dnaServer(textToServer, new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						dialogBox.setText("Remote Procedure Call - Failure");
						serverResponseLabel
								.addStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML(SERVER_ERROR);
						dialogBox.center();
						closeButton.setFocus(true);
					}

					public void onSuccess(String result) {
						dialogBox.setText("Remote Procedure Call");
						serverResponseLabel
								.removeStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML(result);
						dialogBox.center();
						closeButton.setFocus(true);
					}
				});
			}
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();

		DnaHandler dnahandler = new DnaHandler();
		sendButtonNumberOfMarkers.addClickHandler(dnahandler);
		numberOfMarkers.addKeyUpHandler(dnahandler);
		sendButton.addClickHandler(handler);
		btnWy.addClickHandler(dnahandler);
		// fileUpload.setSize("100%", "100%");

		nameField.selectAll();
		nameField.addKeyUpHandler(handler);
	}
}
